package com.wondertek.poms.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.wondertek.poms.common.exception.BusinessException;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.core.service.IClassifyService;
import com.wondertek.poms.core.service.IPcPrdInfoRuleService;
import com.wondertek.poms.dao.po.Classify;
import com.wondertek.poms.dao.po.PrdInfoRule;
import com.wondertek.poms.dao.po.PrdRuleSecondClassify;
import com.wondertek.poms.service.vo.PrdInfoRuleImportVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * @author evan0213
 * @description
 */
@Slf4j
public class UploadDataListener extends AnalysisEventListener<PrdInfoRuleImportVo> {

	private IPcPrdInfoRuleService prdInfoRuleService;
	private IClassifyService classifyService;

	public UploadDataListener(IPcPrdInfoRuleService prdInfoRuleService, IClassifyService classifyService) {
		this.prdInfoRuleService = prdInfoRuleService;
		this.classifyService = classifyService;
	}

	@Override
	public void invoke(PrdInfoRuleImportVo prdInfoRuleImportVo, AnalysisContext analysisContext) {
		log.info("解析到商品包规则数据：{}", prdInfoRuleImportVo);
		PrdInfoRule infoRule = transform(prdInfoRuleImportVo);
		PrdInfoRule rule = prdInfoRuleService.saveRuleAndSecondClassify(infoRule);
		log.info("导入的商品包规则id为{}", rule.getPrdRuleId());
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext analysisContext) {

	}

	/**
	 * 转换规则
	 *
	 * @param vo excel中读取到的
	 * @return 转换后的规则
	 */
	public PrdInfoRule transform(PrdInfoRuleImportVo vo) {
		PrdInfoRule infoRule = new PrdInfoRule();
		Long prdInfoId = vo.getPrdInfoId();
		String type = vo.getIsprdSel();
		if (prdInfoId == null || StringUtils.isBlank(type)) {
			throw new BusinessException(MessageCodes.M989902);
		} else {
			infoRule.setPrdInfoId(prdInfoId);

			if (StringUtils.isNotBlank(type)) {
				switch (type) {
					case "已选规则":
						infoRule.setIsprdSel(2);
						String mediaUsageCode = vo.getMediaUsageCode();
						if (StringUtils.isNotBlank(mediaUsageCode)) {
							infoRule.setMediaUsageCode(mediaUsageCode);
						}
						break;
					case "限制规则":
						infoRule.setIsprdSel(4);
						break;
				}
			}

			String firstClassify = vo.getFirstClassify();
			if (StringUtils.isNotBlank(firstClassify)) {
				StringBuffer buffer = new StringBuffer();
				String[] classifys = firstClassify.split(",");
				for (int i = 0; i < classifys.length; i++) {
					Classify classifyByName = classifyService.findByClassifyName(classifys[i]);
					if (i == 0) {
						buffer.append(classifyByName.getClassifyId());
					} else {
						buffer.append(",").append(classifyByName.getClassifyId());
					}
				}
				infoRule.setFirstClassify(buffer.toString());
			}

			Set<PrdRuleSecondClassify> secondClassifySet = new HashSet<>();
			String secondClassify = vo.getSecondClassify();
			if (StringUtils.isNotBlank(secondClassify)) {
				String[] secondClassifys = secondClassify.split(",");
				for (String classify : secondClassifys) {
					String[] kv = classify.split(":");
					PrdRuleSecondClassify prdRuleSecondClassify = new PrdRuleSecondClassify();
					prdRuleSecondClassify.setPrdRuleId(infoRule.getPrdRuleId());
					prdRuleSecondClassify.setKey(kv[0]);
					prdRuleSecondClassify.setValues(kv[1]);
					prdRuleSecondClassify.setType("1");
					secondClassifySet.add(prdRuleSecondClassify);
				}
			}
			infoRule.setSecondClassifys(secondClassifySet);

			String assist = vo.getAssist();
			if (StringUtils.isNotBlank(assist)) {
				StringBuffer buffer = new StringBuffer();
				String[] assists = assist.split(",");
				for (int i = 0; i < assists.length; i++) {
					Classify assistByName = classifyService.findByClassifyName(assists[i]);
					if (i == 0) {
						buffer.append(assistByName.getClassifyId());
					} else {
						buffer.append(",").append(assistByName.getClassifyId());
					}
				}
				infoRule.setAssist(buffer.toString());
			}

			String category = vo.getCategory();
			if (StringUtils.isNotBlank(category)) {
				StringBuffer buffer = new StringBuffer();
				String[] categories = category.split(",");
				for (int i = 0; i < categories.length; i++) {
					if (i == 0) {
						buffer.append(categories[i]);
					} else {
						buffer.append(",").append(categories[i]);
					}
				}
				infoRule.setCategory(buffer.toString());
			}

			String hasVideo = vo.getHasVideo();
			if (StringUtils.isNotBlank(hasVideo)) {
				if ("是".equals(hasVideo)) {
					infoRule.setHasVideo("1");
				} else {
					infoRule.setHasVideo("0");
				}
			}

			String contentLevel = vo.getContentLevel();
			if (StringUtils.isNotBlank(contentLevel)) {
				infoRule.setContentLevel(contentLevel);
			}

			String formType = vo.getFormType();
			switch (formType) {
				case "剧集的单集":
					infoRule.setFormType("7");
					break;
				case "非剧集":
					infoRule.setFormType("8");
					break;
				case "专辑":
					infoRule.setFormType("9");
					break;
				case "单篇":
					infoRule.setFormType("11");
					break;
				case "图文集":
					infoRule.setFormType("12");
					break;
				case "直播节目":
					infoRule.setFormType("13");
					break;
				case "单片节目壳":
					infoRule.setFormType("14");
					break;
				case "直播节目壳":
					infoRule.setFormType("15");
					break;
				case "融媒体":
					infoRule.setFormType("16");
					break;
			}
			String recommendation = vo.getRecommendation();
			if (StringUtils.isNotBlank(recommendation)) {
				infoRule.setRecommendation(recommendation);
			}

			Long copyRightCpId = vo.getCopyRightCpId();
			if (copyRightCpId != null) {
				infoRule.setCopyRightCpId(copyRightCpId);
			}

			String copyRightScope = vo.getCopyRightScope();
			if (StringUtils.isNotBlank(copyRightScope)) {
				if ("品牌".equals(copyRightScope)) {
					infoRule.setCopyRightScope("1");
				} else if ("全业务".equals(copyRightScope)) {
					infoRule.setCopyRightScope("2");
				} else {
					infoRule.setCopyRightScope("3");
				}
			}

			String copytypeTal = vo.getCopytypeTal();
			if (StringUtils.isNotBlank(copytypeTal)) {
				StringBuffer buffer = new StringBuffer();
				String[] terminals = copytypeTal.split(",");
				for (int i = 0; i < terminals.length; i++) {
					if (i != 0) {
						buffer.append(",");
					}
					switch (terminals[i]) {
						case "全平台":
							buffer.append("1");
							break;
						case "手机终端":
							buffer.append("2");
							break;
						case "PC":
							buffer.append("3");
							break;
						case "IPTV":
							buffer.append("4");
							break;
						case "平板":
							buffer.append("5");
							break;
						case "数字有线网":
							buffer.append("6");
							break;
						case "互联网电视":
							buffer.append("7");
							break;
						case "一体机":
							buffer.append("8");
							break;
						case "投影仪":
							buffer.append("9");
							break;
						case "VR/AR可穿戴设备":
							buffer.append("10");
							break;
						case "智能终端":
							buffer.append("11");
							break;
						case "画屏":
							buffer.append("12");
							break;
						case "专网OTT盒子":
							buffer.append("13");
							break;
					}
				}
				infoRule.setFormType(buffer.toString());
			}

			String copyRightArea = vo.getCopyRightArea();
			if (StringUtils.isNotBlank(copyRightArea)) {
				StringBuffer buffer = new StringBuffer();
				String[] areas = copyRightArea.split(",");
				for (int i = 0; i < areas.length; i++) {
					if (i != 0) {
						buffer.append(",");
					}
					switch (areas[i]) {
						case "内地":
							buffer.append("1");
							break;
						case "香港":
							buffer.append("2");
							break;
						case "澳门":
							buffer.append("3");
							break;
						case "台湾":
							buffer.append("4");
							break;
						case "海外":
							buffer.append("5");
							break;
						case "不限":
							buffer.append("6");
							break;
						case "内蒙古":
							buffer.append("07");
							break;
						case "辽宁":
							buffer.append("08");
							break;
						case "吉林":
							buffer.append("09");
							break;
						case "黑龙江":
							buffer.append("10");
							break;
						case "江苏":
							buffer.append("11");
							break;
						case "浙江":
							buffer.append("12");
							break;
						case "安徽":
							buffer.append("13");
							break;
						case "福建":
							buffer.append("14");
							break;
						case "江西":
							buffer.append("15");
							break;
						case "山东":
							buffer.append("16");
							break;
						case "河南":
							buffer.append("17");
							break;
						case "湖北":
							buffer.append("18");
							break;
						case "湖南":
							buffer.append("19");
							break;
						case "广东":
							buffer.append("20");
							break;
						case "广西":
							buffer.append("21");
							break;
						case "海南":
							buffer.append("22");
							break;
						case "四川":
							buffer.append("23");
							break;
						case "贵州":
							buffer.append("24");
							break;
						case "云南":
							buffer.append("25");
							break;
						case "西藏":
							buffer.append("26");
							break;
						case "陕西":
							buffer.append("27");
							break;
						case "甘肃":
							buffer.append("28");
							break;
						case "青海":
							buffer.append("29");
							break;
						case "宁夏":
							buffer.append("30");
							break;
						case "新疆":
							buffer.append("31");
							break;
						case "重庆":
							buffer.append("32");
							break;
						case "上海":
							buffer.append("33");
							break;
						case "天津":
							buffer.append("34");
							break;
						case "北京":
							buffer.append("35");
							break;

						case "河北":
							buffer.append("36");
							break;
						case "美国":
							buffer.append("37");
							break;
						case "英国":
							buffer.append("38");
							break;
						case "德国":
							buffer.append("39");
							break;
						case "日本":
							buffer.append("40");
							break;
						case "韩国":
							buffer.append("41");
							break;
						case "法国":
							buffer.append("42");
							break;
						case "加拿大":
							buffer.append("43");
							break;
						case "澳大利亚":
							buffer.append("44");
							break;
						case "波兰":
							buffer.append("45");
							break;
						case "泰国":
							buffer.append("46");
							break;
						case "山西":
							buffer.append("47");
							break;

					}
				}
				infoRule.setCopyRightArea(buffer.toString());

			}

			String optimal = vo.getOptimal();
			if (StringUtils.isNotBlank(optimal)) {
				if ("是".equals(optimal)) {
					infoRule.setOptimal(0);
				} else {
					infoRule.setOptimal(1);
				}
			}

			String copyrightType = vo.getCopyrightType();
			if (StringUtils.isNotBlank(copyrightType)) {
				if ("弱版权".equals(optimal)) {
					infoRule.setCopyrightType("2");
				} else {
					infoRule.setCopyrightType("1");
				}
			}

			String needDRM = vo.getNeedDRM();
			if (StringUtils.isNotBlank(needDRM)) {
				if ("是".equals(needDRM)) {
					infoRule.setNeedDRM("0");
				} else {
					infoRule.setNeedDRM("1");

				}
			}

			String authorizationWay = vo.getAuthorizationWay();
			if (StringUtils.isNotBlank(authorizationWay)) {
				switch (authorizationWay) {
					case "单片授权":
						infoRule.setAuthorizationWay("1");
						break;
					case "集体授权":
						infoRule.setAuthorizationWay("2");
						break;
					case "频道授权":
						infoRule.setAuthorizationWay("3");
						break;
					case "三方SDK播放授权":
						infoRule.setAuthorizationWay("4");
						break;
				}
			}

			String freeType = vo.getFreeType();
			if (StringUtils.isNotBlank(freeType)) {
				String[] types = freeType.split(",");
				StringBuffer buffer = new StringBuffer();
				for (int i = 0; i < types.length; i++) {
					if (i != 0) {
						buffer.append(",");
					}
					switch (types[i]) {
						case "不限":
							buffer.append("1");
							break;
						case "收费":
							buffer.append("2");
							break;
						case "仅支持按次":
							buffer.append("3");
							break;
						case "免费":
							buffer.append("4");
							break;
					}
				}
				infoRule.setFreeType(buffer.toString());
			}

			String output = vo.getOutput();
			if (StringUtils.isNotBlank(output)) {
				if ("可输出".equals(output)) {
					infoRule.setOutput(0);
				} else {
					infoRule.setOutput(1);
				}
			}


		}

		return infoRule;
	}

}
